package com.test.spring.thread;

import java.util.concurrent.TimeUnit;

/**
 * @author ll
 * @Date 2017/10/24 17:54
 */
public class NoVisibility {
    static boolean isRunning = true;//共享变量增加volatile关键字修饰进程会在5S后结束，而不加则不会结束

    public static void main(String[] args) throws InterruptedException {
        Thread runningT = getRunningThread();

        runningT.start();

        TimeUnit.SECONDS.sleep(5);

        isRunning = false;//注意： main Thread 执行到此,预期runningThread 应该结束
    }

    public static Thread getRunningThread() {
        return new Thread(new Runnable() {

            @Override
            public void run() {
                //synchronized ((Object) isRunning) {
                    while (isRunning) {
                        System.out.println(isRunning);
                    }
                //}
            }
        }, "RunningThread");
    }
}
